//请求开发环境的服务器的axios实例
import axios from "axios"
import nProgress from "nprogress";
import "nprogress/nprogress.css";

//创建实例
const request = axios.create({
    baseURL:process.env.VUE_APP_MOCKAPI,
    timeout:9999,
});


//给实例配置拦截器
request.interceptors.request.use(
    (config) => {
      //发送请求触发进度条启动
      nProgress.start();
  
      return config;
    },
    (error) => {
      //触发响应以后，进度条结束
      nProgress.done();
      return Promise.reject(error);
    }
  );
  
  request.interceptors.response.use(
    (response) => {
      //触发响应以后，进度条结束
      nProgress.done();
  
      if (response.data.code === 200) {
        return response.data.data;
      } else {
        //自己封装一个错误对象
        return Promise.reject({ message: response.data.message });
      }
    },
    (error) => {
      //触发响应以后，进度条结束
      nProgress.done();
      return Promise.reject(error);
    }
  );

export default request;